// 登录UI组件

import 'package:flutter/material.dart';

import 'package:my_first_app/utils/app_logger.dart';
import 'package:my_first_app/screen/show_screen.dart';

class ComponentLogin extends StatefulWidget {
  const ComponentLogin({super.key});

  @override
  State<ComponentLogin> createState() => _LoginPageState();
}

class _LoginPageState extends State<ComponentLogin> {
  final TextEditingController _usernameController = TextEditingController();
  final TextEditingController _passwordController = TextEditingController();
  bool _isPasswordVisible = false;

  final logger = AppLogger();

  @override
  Widget build(BuildContext context) {
    // logger.info('用户未登录，跳转到登录界面');

    return Scaffold(
      appBar: AppBar(
        title: const Text('登录'),
        centerTitle: true,
      ),
      body: Padding(
        padding: const EdgeInsets.all(20.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            // 标题
            const Text(
              '欢迎登录',
              style: TextStyle(
                fontSize: 24,
                fontWeight: FontWeight.bold,
              ),
            ),
            const SizedBox(height: 40),

            // 用户名输入框
            TextField(
              controller: _usernameController,
              decoration: const InputDecoration(
                labelText: '用户名',
                hintText: '请输入用户名',
                prefixIcon: Icon(Icons.person),
                border: OutlineInputBorder(),
              ),
            ),
            const SizedBox(height: 20),

            // 密码输入框
            TextField(
              controller: _passwordController,
              obscureText: !_isPasswordVisible,
              decoration: InputDecoration(
                labelText: '密码',
                hintText: '请输入密码',
                prefixIcon: Icon(Icons.lock),
                suffixIcon: IconButton(
                  icon: Icon(
                    _isPasswordVisible ? Icons.visibility : Icons.visibility_off,
                  ),
                  onPressed: () {
                    setState(() {
                      _isPasswordVisible = !_isPasswordVisible;
                    });
                  },
                ),
                border: OutlineInputBorder(),
              ),
            ),
            const SizedBox(height: 30),

            // 登录按钮
            SizedBox(
              width: double.infinity,
              child: ElevatedButton(
                onPressed: () {
                  final username = _usernameController.text;
                  final password = _passwordController.text;
                  // 模拟联网校验
                  logger.info('当前用户输入用户名为[$username], 输入密码为[$password]');
                  if (username == "root" && password == "123"){
                    logger.info('用户身份验证正确');
                    // 页面跳转
                    Navigator.push(
                      context,
                      MaterialPageRoute(
                        builder: (context) => ShowScreen(),
                      ),
                    );

                  } else {
                    logger.info('用户身份验证无效');

                    // 类似于原生AP Toast 风格
                    ScaffoldMessenger.of(context).showSnackBar(
                        SnackBar(
                          content: const Text('用户名或密码错误'),
                          duration: const Duration(seconds: 2),
                          behavior: SnackBarBehavior.floating,
                        ),
                    );

                  }
                },
                child: const Text(
                  '登 录',
                  style: TextStyle(fontSize: 16),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  @override
  void dispose() {
    _usernameController.dispose();
    _passwordController.dispose();
    super.dispose();
  }
}